const { Promise } = require('es6-promise')

exports.install = function(Vue, options) {
  // 普通提示框
  Vue.prototype.$msg_ok = function(msg, title) {
    this.$message({
      type: msg,
      message: title
    })
  }
  // 删除选择提示框
  Vue.prototype.$msg_confirm = function() {
    return this.$confirm('确认要删除吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      this.$message({
        type: 'success',
        message: '删除成功'
      })
      return Promise.resolve(true)
    }).catch(() => {
      this.$message({
        type: 'info',
        message: '已取消删除'
      })
      return Promise.reject(false)
    })
  }
  // 选择提示框
  Vue.prototype.$msg_confirm_show = function(msg, title,input) {
    // eslint-disable-next-line space-infix-ops
    title=title||'提示'
    return this.$confirm(msg, title, {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      if(!input){
        this.$message({
          type: 'success',
          message: '操作成功'
        })
      }
      return Promise.resolve(true)
    }).catch(() => {
      this.$message({
        type: 'info',
        message: '操作已取消'
      })
      return Promise.reject(false)
    })
  }
  Vue.prototype.$msg_firm = function(msg, title) {
    return this.$confirm(msg, title, {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      return Promise.resolve(true)
    }).catch(() => {
      return Promise.reject(false)
    })
  }
  Vue.prototype.$copy = function(text) {
    var textarea = document.createElement("textarea");
    var currentFocus = document.activeElement; 
    document.body.appendChild(textarea); 
    textarea.value = text;
    textarea.focus();
    if(textarea.setSelectionRange)
        textarea.setSelectionRange(0, textarea.value.length); 
    else
        textarea.select();
    try {
        var flag = document.execCommand("copy"); 
    } catch(eo) {
        var flag = false;
    }
    document.body.removeChild(textarea); 
    currentFocus.focus();
  }
  Vue.prototype.$beforeAvatarUploadMb = function(file) {
    let size=1024
    const isJPG = file.type === 'image/jpeg'||file.type === 'image/png'
      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!')
        return false
      }
      const isLt2M = file.size  < size*1024 * 1024
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 '+size+'KB!')
      }
      return isLt2M&&isJPG
  }

  Vue.prototype.$beforeAvatarUpload = function(file,size) {
    size=size||512
    const isJPG = file.type === 'image/jpeg'||file.type === 'image/png'
      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!')
        return false
      }
      
      const isLt2M = file.size  < size*1024
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 '+size+'KB!')
      }
      return isLt2M&&isJPG
  }
}

